﻿// @include "code/Browser.js"
// @include "code/Type.js"
// @include "extend/Debug.js"
/**
 * @fileOverview 异步加载工具
 * 
 * @version qinglan_Import 1.0
 * @static
 * @author wxq wxqqh@qq.com
 */
/**
 * 命名空间
 * 
 * @namespace
 * @type Object
 */
window.qinglan = window.qinglan || {};
/**
 * 异步加载js组件
 * 
 * @type Object
 */
qinglan.Import = qinglan.Import || {};
/**
 * 加载js文件
 * 
 * @param {String}
 *            src js文件请求地址
 * @param {Function}
 *            callback 回调函数
 * @param {Object}
 *            param 回调函数参数
 * 
 */
qinglan.Import.js = function(src, callback, param) {
	var d = document;
	var scriptTag = document.createElement("script");
	scriptTag.src = src;
	d.body.appendChild(scriptTag);
	if (qinglan.Type.isUndefined(callback)) {
		scriptTag.onload = function() {
			try {
				callback.call(window, param);
			} catch (e) {
				qinglan.Debug.error('Load js file : [ ' + src + ' ]faild ');
			}
		};
		scriptTag.onreadystatechange = function() {
			if (this.readyState == "complete" || this.readyState == "loaded") {
				try {
					callback.call(window, param);
				} catch (e) {
					qinglan.Debug.error('Load js file : [ ' + src + ' ]faild ');
				}
			}
		};
	}
};
/**
 * 引入css文件
 * 
 * @param {String}
 *            src css文件请求地址
 */
qinglan.Import.css = function(src) {
	var node = document.createElement("link");
	node.rel = "stylesheet";
	node.type = "text/css";
	node.href = src;
	document.getElementsByTagName("head")[0].appendChild(node);
};
/**
 * 引入图片文件
 * 
 * @param {String}
 *            src 图片地址
 * @param {Function}
 *            callback
 * @return {Image} 图片对象
 */
qinglan.Import.img = function(src, callback) {
	var img = new Image();
	img.onload = function() {
		callback.call(img);
		this.onload = null;
	};
	img.src = src;
	return img;
};